var afterLogId = "10000000000000000000000000000000";			//已经查出来的日志id的最大值
$(function(){	
	
	var curStepId = null;
	
	var userName = null; // 用户名
	
	var userRoles = ""; // 用户角色
	
	$("#changeViceCommender").click(function() {
		 $("#leaderChooseWindow").dialog("open");
	});
	
	$('#nextStep').attr(
			"disabled", true);
	
	$('#send').attr(
			"disabled", true);
	
	$('#addStep').attr(
			"disabled", true);
	
	
	
	//当点击下一环节时,要获取下一环节的id 等一秒钟后才把第一个步骤的内容查出来,在等2秒，才可以点击下一个环节
	setTimeout(function()
		{
		$('#content').attr('readOnly',true);
		$('#mess').focus();
		//取得当前用户信息
		findUserInfo();
		
		//进入页面时先获取action中的日志最大id，判断该页面是否被打开过
		//	initLogId = myrpc.getUpdateLogId();
			//alert("initLogId:"+initLogId);
			//if(initLogId=="10000000000000000000000000000000")
		initContents();
		initLogType();
		//getLogContent();
		//拿到当时环节ＩＤ
		curStepId = $(parent.window.frames['topMenuFrame'].document).find('.currentStep').attr('id');
		//alert(curStepId);
			setInterval(getLogContent, 5000); 
			//检查是否清空对话框
			setInterval(clearContent, 1000); 
			
			function getLogContent()
			{
				var maxTime = $('#maxTime').text();
				var timestamp = Date.parse(new Date());
				var eventId = $('#eventId').text();
				//alert(maxTime);
				$.ajax({
					type: "post",
					url: "/ag/lwc/findStepLog_CurrentPsContentAction.action?ts="+timestamp,
					data: {maxTime:maxTime,eventId:eventId},
					success:function(data){
						
						if(data.logList != null) {
							$.each(data.logList,function(i,n){
								var newDate = stringToDate(n.logTime);
								//alert(newDate);
								$('#maxTime').text(newDate);
								var ary = getNameAndContent(n.logContent);
								//alert(n.logTime);
								$("<p>【"+n.tbLogType.ltName+"】<br>" +ary[0]+" :&nbsp;&nbsp;"+newDate+"<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;"+ary[1]+"</p>").appendTo($("#content"));
								$("#content")[0].scrollTop = $("#content")[0].scrollHeight;
	  				
							});
						}
					}
				});

					//$("<p>【"+logs[i].tbLogType+"】:&nbsp;&nbsp;"+logs[i].logTime+"<br>"+logs[i].logContent+"</p>").appendTo($("#content"));		//将更新的日志发送到屏幕上
	  		}
			
			setTimeout(enablebtn,4000);
			setTimeout(function(){$("#nextStep").bind("click",getNext);},2000)},3000); 
	
//	$("#nextStep").click(function(){
//		var eventId = $('#eventId').text();
//		//alert(eventId);
//		updateCurrentStepNum(eventId);
//	});
	
	function findUserInfo(){	
		var timestamp = Date.parse(new Date());
		$.get("/ag/admin/UserServ_findUserInfo.action?ts="+timestamp,null,function(data){
			userName = data.tbUser.UName;
			//alert(userName);
			//alert(data.tbUser.tbUserRoles);
			findUserRoles(data);//tbUser\.tbUserRoles\[\d+\]\.tbRole\.RName
		});
	}
	
	function findUserRoles(data) {
		$.each(data.tbUser.tbUserRoles,function(i,r){
			//alert(r.tbRole.RName);
			userRoles = userRoles+r.tbRole.RName+"|";
		});
		//alert(userRoles);
	}
	
	
	/*
	 * 日期转换
	 */
	function stringToDate(date) {
		
		var dateAndTime = date.split("T");
		
		//alert(a);
		
		var newDate = dateAndTime[0]+" "+dateAndTime[1];
		
		//alert(b);
		return newDate;
	}
	/*
	 * 清空显示内容框
	 */
	function clearContent() {
		var newCurStepId = $(parent.window.frames['topMenuFrame'].document).find('.currentStep').attr('id');
		//alert(newCurStepId+"=?"+curStepId);
		if(newCurStepId != curStepId && newCurStepId != null) {
			$('#send').attr(
					"disabled", true);
			curStepId = newCurStepId;
			$("#content").text("");
			initContents();
		}
	}
	
//	function checkCurIdChange(){
//		setInterval(function(){
//			if()
//		},3000);
//	}
	
	
	//将第一个环节的内容查出来,放到发送的内容中
	function initContents()
	{
		$('#send').attr(
				"disabled", true);
		var timestamp = Date.parse(new Date());
		var eventId = $('#eventId').text();
		$.ajax({
		  		type: "post",
		 		url: "/ag/lwc/getContents_CurrentPsContentAction.action?ts="+timestamp,
		 		data: {eventId:eventId},
		  		success:function(data){
				tips = new Array();
		  			$.each(data.currentContents,function(i,n){
		  			//	alert(n.pscContent);
		  				tips[tips.length]=n.pscContent;
					});
		  		}
			});
		$('#send').attr(
				"disabled", false);
	}
	//初始化日志类型选项
	function initLogType()
	{
		var typeSelect = $("#myselect");
		var timestamp = Date.parse(new Date());
		$.ajax({
	  		type: "post",
	 		url: "/ag/lwc/findLogType_CurrentPsContentAction.action?ts="+timestamp,
	  		success:function(data){
		
	  			$.each(data.logTypeList,function(i,n){
	  			//	alert("日志类型id："+n.ltId);
	  				$("<option value="+n.ltId+" >"+n.ltName+"</option>").appendTo(typeSelect);
				});
	  		}
		});
	}
	//点击下一个环节的事件
	function getNext(){
		//隔两秒之后获取当前环节的内容
		
		setTimeout(function(){
		//	initContents();
			var timestamp = Date.parse(new Date());
			var eventId = $('#eventId').text();
			$.ajax({
		  		type: "post",
		 		url: "/ag/lwc/getContents_CurrentPsContentAction.action?ts="+timestamp,
		 		data:{eventId:eventId},
		  		success:function(data){
				tips = new Array();
		  			$.each(data.currentContents,function(i,n){
		  				//alert(n.pscContent);
		  				tips[tips.length]=n.pscContent;
					});
		  		}
			});
			
		},3000)
	}
	
	
	//发送
	$("#send").click(send);
	$("#mess").bind('keydown', 'ctrl+enter', send);
	
	function send(){					//发送
		var mess = /*$.trim(*/$("#mess").val()/*)*/;			//获取信息
		mess = userRoles +" : "+ userName +"#@$%"+ mess;
	//alert(mess);
		var option = $("#myselect").val();		//获取选项,日志类型
		if(mess=="") {
			alert("发送内容不可为空！");
			$('#mess').focus();
			return false;
		}
	//alert("日志类型："+option);
		var timestamp = Date.parse(new Date());
		var eventId = $('#eventId').text();
		var url="/ag/lwc/send_SendLogAction.action?ts="+timestamp;
		var req={mess:mess,logtype:option,eventId:eventId};
		var ajax=jQuery.post(url,req,function (data)
				{
				if((typeof data.message) == "undefined")
					data = eval("("+data+")");
			//	alert("发送结果："+data);
				});
		$("#content")[0].scrollTop = $("#content")[0].scrollHeight;
		oPopDiv.hide();
			$("#mess").text("").trigger("blur");
		$('#mess').focus();
	}
	/*
	 * 分离用户各和日志内容
	 */
	function getNameAndContent(str){
		var ss = str.split("#@$%");
		return ss;
	}
	
	function enablebtn() {
		$('#nextStep').attr(
				"disabled", false);
		
		$('#send').attr(
				"disabled", false);
		
		$('#addStep').attr(
				"disabled", false);
	}
	
//	/*更新当前环节序号*/
//	function updateCurrentStepNum(eventId) {
//		var data = {
//			eventId : eventId
//		};
//		$.ajax( {
//			url : "/ag/zrt/updateCurrentStepNum_CurrentEvent.action",
//			type : "POST",
//			data : data
//		});
//	}
});
